package com.bjx.file.controller;

import com.bjx.common.core.domain.R;
import com.bjx.file.domain.vo.FileVO;
import com.bjx.file.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @desc: 文件控制器
 * @author: liushuai
 * @date: 2023/5/15 14:19
 */
@RestController
public class FileController {

    @Resource
    private FileService fileService;


    /**
     * 单个文件上传
     * @param file
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2023/5/15 15:01
     */
    @Operation(summary = "单个文件上传")
    @GetMapping("upload")
    public R upload(MultipartFile file,@RequestParam(value = "open",required = false) Boolean open){
        FileVO fileVo = fileService.upload(file,open);
        return R.success(fileVo);
    }
    /**
     * 根据文件存储的url获取文件预览url
     * @param url
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2023/5/15 18:23
     */
    @Operation(summary = "根据文件存储的url获取文件预览url")
    @GetMapping("preview")
    public R view(@RequestParam("url") String url,@RequestParam(value = "open",required = false) Boolean open){
        FileVO fileVo = fileService.preview(url,open);
        return R.success(fileVo);
    }

    /**
     * 根据文件存储的url下载文件
     * @param httpServletResponse
     * @param url
     * @param open
     * @Author: liushuai
     * @Date: 2023/5/16 21:42
     */
    @Operation(summary = "根据文件存储的url下载文件")
    @GetMapping("download")
    public void download(HttpServletResponse httpServletResponse, @RequestParam("url") String url, @RequestParam(value = "open",required = false) Boolean open){
        fileService.download(httpServletResponse,url,open);
    }

    /**
     * 根据文件存储的url删除文件
     * @param url
     * @param open
     * @Author: liushuai
     * @Date: 2023/5/16 21:56
     */
    @Operation(summary = "根据文件存储的url删除文件")
    @GetMapping("delete")
    public R delete(@RequestParam("url") String url, @RequestParam(value = "open",required = false) Boolean open){
        fileService.delete(url,open);
        return R.success();
    }
}
